/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef LOTTIEPRO_LOTTIE_RENDER_NODE_H
#define LOTTIEPRO_LOTTIE_RENDER_NODE_H

#include "brush.h"
#include "canvas.h"
#include "draw_filter.h"
#include "path.h"
#include "rlottiecommon.h"
#include <native_drawing/drawing_types.h>

namespace LottiePro {

struct ColorPercent {
    float r;
    float g;
    float b;
    float opacity;
};

/**
 * @isAllMask : is open all mask
 * @width     : stroke width
 * @opacity   : line opacity
 * @lineSpace : line space
 * @showType  : show type 1: background 2:opacity 3:line color 4:displays the original
 * @colors    : line color
 */
struct StrokeProps {
    int isAllMask;
    float width;
    float opacity;
    float lineSpace;
    int showType;
    std::vector<float> colors;
};

enum StrokeShowType {
    /**
     * 画布展示layer背景颜色，同时stroke展示设置的颜色
     */
    DISPLAY_BACKGROUND = 1,
    /**
     * 画布展示透明颜色，同时stroke展示设置的颜色
     */
    DISPLAY_OPACITY = 2,
    /**
     * 画布展示透明颜色，同时stroke展示设置layer的颜色
     */
    DISPLAY_LINE = 3,
};

class LottieRenderNode {
public:
    LottieRenderNode();
    ~LottieRenderNode();
    /**
     * 处理单个节点（路径、填充等）
     * @param canvas
     * @param node
     * @param clipMask
     */
    void ProcessNode(std::shared_ptr<drawing::Canvas> canvas, const LOTNode *node,
                     std::shared_ptr<drawing::Path> clipMask = nullptr);

    /**
     * 设置画刷
     * @param canvas
     * @param node
     */
    void SetupBrush(std::shared_ptr<drawing::Canvas> canvas, const LOTNode *node);

    /**
     * 设置画笔
     * @param canvas
     * @param node
     */
    void SetupPen(std::shared_ptr<drawing::Canvas> canvas, const LOTNode *node);

    /**
     * 绘制路径
     * @param ptPtr
     * @param ptCount
     * @param elmPtr
     * @param elmCount
     * @return
     */
    void ConvertLottiePathToDrawingPath(const float *ptPtr, size_t ptCount, const char *elmPtr, size_t elmCount,
                                        drawing::Path &path);

    /**
     * 高斯模糊
     * @param rawCanvas
     * @param rx x轴方向模糊的偏移量
     * @param ry y轴方向模糊的偏移量
     * @return
     */
    std::shared_ptr<drawing::DrawFilter> SetBluer(std::shared_ptr<drawing::Canvas> rawCanvas, float rx, float ry);

    /**
     * 设置滤镜颜色
     * @param percent
     */
    void SetEffectColorPercent(std::shared_ptr<ColorPercent> percent)
    {
        colorPercent_ = std::move(percent);
    }

    /**
     *  黑白颜色组
     * @param color_Black
     * @param color_White
     * @param ins
     */

    void SetTintColor(std::vector<float> color_Black, std::vector<float> color_White, float ins)
    {
        colorBlack = std::move(color_Black);
        colorWhite = std::move(color_White);
        intensity = ins / 100.0f;
    }

    /**
     * 三色调
     * @param color
     */
    void SetTriToneColor(std::vector<std::vector<float>> color)
    {
        triToneColor = std::move(color);
    }

    /**
     * 设置stroke props
     * @param percent
     */
    void SetEffectStrokeProps(std::shared_ptr<StrokeProps> props)
    {
        strokeProps_ = std::move(props);
    }
    
    /**
     * 清除layer层级ef属性设置
     */
    void ClearEffectOfEf();

private:
    /**
     * 处理路径节点
     * @param canvas
     * @param node
     * @param clipMask
     */
    void ProcessPathNode(std::shared_ptr<drawing::Canvas> canvas, const LOTNode *node,
                         std::shared_ptr<drawing::Path> clipMask);

    /**
     * 处理文本节点
     * @param canvas
     * @param node
     */
    void ProcessTextNode(std::shared_ptr<drawing::Canvas> canvas, const LOTNode *node);

    /**
     * 处理图像节点
     * @param canvas
     * @param node
     */
    void ProcessImageNode(std::shared_ptr<drawing::Canvas> canvas, const LOTNode *node);

    /**
     * 统一设置AttachBrush
     */
    void AttachBrush(std::shared_ptr<drawing::Canvas> canvas);

    /**
     * 设置TriToneEffect效果
     */
    void TriToneEffect(std::shared_ptr<drawing::Canvas> rawCanvas);

    /**
     * 设置TintEffect效果
     */
    void TintEffect(std::shared_ptr<drawing::Canvas> rawCanvas, std::vector<float> color_Black,
                    std::vector<float> color_White, float ins);

    /**
     * 设置StrokeEffect效果
     */
    void StrokeEffect(std::shared_ptr<drawing::Canvas> rawCanvas, std::shared_ptr<drawing::Path> rawClipMask);

    /**
     * 是否走StrokeEffect流程
     */
    bool isRenderStroke();

    std::shared_ptr<drawing::Brush> brush_;
    std::shared_ptr<drawing::Pen> pen_;
    std::shared_ptr<drawing::Path> path_;
    bool isDetachPen{true};
    std::shared_ptr<ColorPercent> colorPercent_{nullptr};
    std::vector<float> colorBlack;
    std::vector<float> colorWhite;
    std::vector<std::vector<float>> triToneColor;
    float intensity;
    std::shared_ptr<StrokeProps> strokeProps_{nullptr};
};

} // namespace LottiePro

#endif // LOTTIEPRO_LOTTIE_RENDER_NODE_H
